package com.itheima.mall.web.ums;

import com.itheima.mall.commons.page.PageResult;
import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.ums.UmsAdminService;
import com.itheima.mall.entity.domain.query.ums.AdminQuery;
import com.itheima.mall.entity.domain.ums.Admin;
import com.itheima.mall.entity.domain.ums.Role;
import com.itheima.mall.entity.domain.vo.ums.AdminVo;
import com.itheima.mall.sso.api.SsoService;
import com.itheima.mall.sso.pojo.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/admin")
public class UmsAdminController {
    @DubboReference
    private SsoService ssoService;

    @DubboReference
    private UmsAdminService adminService;

    /**
     * 登录
     * @param loginUser
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginUser loginUser) {
        LoginUser login = ssoService.login(loginUser);
        System.out.println(login.toString());
        if (login == null) {
            return Result.error();
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("token", login.getToken());
        map.put("tokenHead","T");
        return Result.success(map);
    }

    @PostMapping("/register")
    public Result register(@RequestBody Admin admin){
        adminService.save(admin);
        return Result.success();
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        adminService.delete(id);
        return Result.success();
    }

    /**
     * 获取登录用户信息
     *
     * @param request
     * @return
     */
    @GetMapping("/info")
    public Result info(HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        int index = authorization.indexOf("T");
        String token = authorization.substring(index + 1);
        String username = ssoService.parseToken(token);
        log.info("当前登录用户：" + username);
        AdminVo adminVo = adminService.info(username);
        return Result.success(adminVo);
    }

    /**
     * 用户分页查询
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public Result list(String keyword, Integer pageNum, Integer pageSize) {
        PageResult pageResult = adminService.pageInfo(keyword, pageNum, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 分配用户角色
     *
     * @param adminQuery
     * @return
     */
    @PostMapping("/role/update")
    public Result roleUpdate(AdminQuery adminQuery) {
        adminService.roleUpdate(adminQuery);
        return Result.success();
    }

    /**
     * 获取指定用户的角色
     *
     * @param adminId
     * @return
     */
    @GetMapping("/role/{adminId}")
    public Result role(@PathVariable Integer adminId) {
        List<Role> roles = adminService.role(adminId);
        return Result.success(roles);
    }

    /**
     * 修改用户信息
     *
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public Result update(@PathVariable Integer id,@RequestBody Admin admin) {
        adminService.adminUpdate(admin);
        return Result.success();
    }

    /**
     * 修改密码
     * @param map
     * @return
     */
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Map<String, Object> map) {
        adminService.updatePassword(map);
        return Result.success();
    }

    /**
     * 修改用户状态
     * @param id
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public Result updateStatus(@PathVariable Integer id,Integer status){
        Admin admin = new Admin();
        admin.setId(id);
        admin.setStatus(status);
        adminService.updateStatus(id,admin);
        return Result.success();
    }

    /**
     * 获取指定用户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result admin(@PathVariable Integer id){
       Admin admin = adminService.admin(id);
       return Result.success(admin);
    }
}
